package com.shisfish.library.Prere;

import java.util.Date;
import java.util.List;

import com.shisfish.library.Impl.UserImpl;
import com.shisfish.library.Model.BookLend;
import com.shisfish.library.Model.User;
import com.shisfish.library.Model.UserSingleLend;

public class BeforeBorrow {

	// false 是可借
	public static User isOverdueOrQuota(String employee) {
		
		boolean isOverdueOrQuota = false;
		
		User lastUser = new User();
		
		List<User> users = new UserImpl().selectByKey(employee);
		
		if (users.size() > 0) {
			User user = users.get(0);
			
			List<BookLend> bookLends = user.getBookLends();
			
			if (bookLends.size() > 0) {
				
				Date date = new Date();
				
				for (BookLend bookLend : bookLends) {
					
					if (bookLend.getActualReturnTime() == null && date.after(bookLend.getShouldReturnTime())) {
						isOverdueOrQuota = true;
						break;
					}
					
				}
				
			}
			
			if (!isOverdueOrQuota) {
				// 书有木有接满
				if (user.getUserSingleLend().getBorrow_number() > 0) {
					UserSingleLend userSingleLend = user.getUserSingleLend();
					int borrow = userSingleLend.getBorrow_number();
					int borrowed = userSingleLend.getBorrowed_number();
					
					if (borrowed >= borrow) {
						// 不可借
						isOverdueOrQuota = true;
					}
				}
				else {
					
					// 没有借书历史， 可借
					isOverdueOrQuota = false;
					
					/*UserLevel userLevel = user.getUserLevel();
					
					UserSingleLend userSingleLend = new UserSingleLend(employee, 0, 0, userLevel.getBorrow_numbers(), userLevel.getLoan_numbers(), user.getLoanid());
					
					int flag = new UserSingleLendImpl().insertOne(userSingleLend);
					
					if (flag == 1) {
						// 可借
						isOverdueOrQuota = false;
					}*/
				}
				
			}
			
			// 如果可借，把user传过去，否则，user为空
			if (!isOverdueOrQuota) {
				lastUser = user;
			}
			
		}
		
		return lastUser;
	}
	
}
